var Tool = require('common/tool');
var Config = require('common/config');

require('sass/reset.scss');
require('sass/tool.scss');
Tool.avalonForIe7(avalon);

/* 获取url的参数 */
function queryURLParams() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
       var str = url.substr(1);
       var strs = str.split("&");
       for(var i = 0; i < strs.length; i ++) {
          theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
       }
    }
    return theRequest;
}

require('./index.scss');

require('comp/footerCopyright');
avalon.validators.validate = {
    message: '必须是四位数字验证码',
    get: function (value, field, next) {
        //想知道它们三个参数是什么,可以console.log(value, field,next)
        var ok = String(value).length===4;
        next(ok)
        return value
    }
}
var parameter = queryURLParams();
var loginError = {
    "0002":"系统错误，请联络客户服务专员。",
    "0003":"验证码输入有误，请重新输入！",
    "0004":"你输入的账户或密码错误，请重新输入！",
    "0005":"您没有用户名登录的权限！",
    "0006":"您的账户因连续输错%s次密码已被锁定，请联系{1}或重置密码。[%s]",
    "0007":"您的账户当前状态不允许登录，请联系在线客服！"
};

var vm = avalon.define({
    $id: 'page',
    loading: false,
    login:{
        account:'',
        accountError:'',
        pwd:'',
        pwdError:'',
        randomCodeSrc: '/api/checkCode/service.do?random='+Math.random(),
        validaCode:'',
        validaCodeError:''
    },
    switchCode(){
        this.login.randomCodeSrc = '/api/checkCode/service.do?random='+Math.random();
    },
    /* 触发登录表单整体验证规则 */
    triggerValida:function(){
        vm.validate.onManual();
        return false;
    },
    /* 登录 */
    loginSubmit:function(){
        var that = this;
        vm.loading = true;
        Tool.ajax({
            url:"/loginSubmit",
            type:'post',
            data:{
                username:this.login.account,
                password:this.login.pwd,
                codedata:this.login.validaCode
            }
        }).done(function(res){
            vm.loading = false;
            var code = res.code;
            if(code==="0"){
                var anchor = parameter.url || '';
                Tool.href('/index.html'+decodeURIComponent(anchor));
            }else{
                that.switchCode();
                layer.alert(loginError[code] || res.msg);
            }
        }).error(function(){
            vm.loading = true;
            that.switchCode();
        });
    },
    /* 重置 */
    resetForm(){
        this.login = {
            account:'',
            accountError:'',
            pwd:'',
            pwdError:'',
            randomCodeSrc: '/api/checkCode/service.do?random='+Math.random(),
            validaCode:'',
            validaCodeError:''
        };
        return false;
    },
    validate: {
        //禁止提交时自动验证
        validateAllInSubmit: false,
        onManual:avalon.noop,//IE6-8必须指定,avalon一会儿会重写这方法
        /* 整体验证后触发 */
        onValidateAll: function (reasons) {
            console.log('onValidateAll',reasons);
            if( reasons.length>0 ) {
                reasons.forEach(function(error,index){
                    var _id = reasons[index].element.id;
                    vm.login[_id+"Error"] = error.message;//打印所有错误
                });
                return false;
            }
            vm.loginSubmit();
        },
        onError:function(reasons){
            if(!reasons || reasons.length===0) return false;
            var _id = reasons[0].element.id;
            var a = reasons.map(function (el) {
                return  el.getMessage()
            });
            vm.login[_id+"Error"] = a.join(', ');//打印所有错误
        },
        onSuccess:function(reasons){
            var _id = reasons[0].element.id;
            vm.login[_id+"Error"] = '';
        }
    }
});
$(function(){
    $("#language")
        .text(_language==='zh'?'繁':'简')
        .on('click',function(){
            var switchLanguage = _language==='zh' ? '_tw':'';
            docCookies.setItem('_language', switchLanguage.replace('_',''), Infinity, '/');
            var reg = new RegExp(switchLanguage+'(\.html)');
            location.href= location.href.replace(reg,'$1');
        });

    
});
/* 测试代码 */
/* var loginInfo = Tool.ajax({
    url:'/api/logi.json'
});
console.log(loginInfo);
loginInfo.done(function(res){
    layer.alert(res);
}).error(function(){
    alert('error');
});

console.log('loginInfo',loginInfo); */







